package com.createidea.cms.struts.action.admin;

import java.util.List;

import com.createidea.cms.service.admin.AdminService;
import com.createidea.cms.struts.action.BaseAction;
import com.createidea.cms.to.AdminTo;

public class AdminLoginAction extends BaseAction {
   private AdminTo admin;
   private List<AdminTo> admins;
   private AdminService adminService;
   private String adminId;
   private String adminNewPwd;
    
	public String loadLoginInfo(){
		return SUCCESS;
	}
	
	public String adminLogin(){
		AdminTo adminFromDB = adminService.getAdminByName(admin.getAdminName());
		if(adminFromDB == null || (adminFromDB != null && !adminFromDB.getAdminPwd().equals(admin.getAdminPwd()))) {
			this.addActionError("用户不存在或者密码错误，请重新输入！");
			return INPUT;
		} else {
			return SUCCESS;
		}
	}
	
	public String loadheader() {
		return SUCCESS;
	}
	
	public String loadleft(){
		return SUCCESS;
	}
	
	public String loadmain(){
		return SUCCESS;
	}
	
	public String loadUserlist() {
		return SUCCESS;
	}
	
	public String editUser() {
		return SUCCESS;
	}
	
	public String maintainAdmin() {
		admins = adminService.getAllAdmins(1);
		return SUCCESS;
	}
	
	public String addAdmin() {
		return SUCCESS;
	}
	
	public String saveAdmin() {
		adminService.saveAdmin(admin);
		return SUCCESS;
	}
	
	public String deleteAdmin() {
		adminService.deleteAdmin(adminId);
		return SUCCESS;
	}
	
	public String editAdmin() {
		admin = adminService.getAdminById(adminId);
		return SUCCESS;
	}
	
	public String updateAdmin() {
		AdminTo adminFromDB = adminService.getAdminByName(admin.getAdminName());
		if(admin.getAdminPwd().equals(adminFromDB.getAdminPwd())) {
			admin.setAdminPwd(adminNewPwd);
			adminService.saveOrUpdateAdmin(admin);
			return SUCCESS;
		} else {
			this.addActionError("原密码输入不正确，请重新输入！");
			return INPUT;
		}
	}

	public AdminTo getAdmin() {
		return admin;
	}

	public void setAdmin(AdminTo admin) {
		this.admin = admin;
	}

	public List<AdminTo> getAdmins() {
		return admins;
	}

	public void setAdmins(List<AdminTo> admins) {
		this.admins = admins;
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public String getAdminId() {
		return adminId;
	}

	public void setAdminId(String adminId) {
		this.adminId = adminId;
	}

	public String getAdminNewPwd() {
		return adminNewPwd;
	}

	public void setAdminNewPwd(String adminNewPwd) {
		this.adminNewPwd = adminNewPwd;
	}

}
